package com.example.hbutliving.config.swagger;



import com.example.hbutliving.util.JwtUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author lizijian
 */
@Configuration
@EnableSwagger2WebMvc
public class Swagger2 {

    @Autowired
    private JwtUtil jwtUtil;
    @Bean
    public Docket createRestApi(ServletContext servletContext) {
        /*添加接口请求头参数配置 没有的话 可以忽略*/
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        String userToken = jwtUtil.createUserToken(1);
        tokenPar.name("token").description("令牌").defaultValue(userToken).modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.hbutliving.controller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("布咕星球api")
                .description("")
                .version("1.0")
                .build();
    }
}
